package com.sinoglobal.ningxia.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.ab.view.listener.AbOnListViewListener;
import com.ab.view.pullview.AbPullListView;
import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.activity.mico.GSYVDHActivity;
import com.sinoglobal.ningxia.adapter.ProgramHistoryAdapter;
import com.sinoglobal.ningxia.beans.ProgramListVo;
import com.sinoglobal.ningxia.beans.programInfoVo;
import com.sinoglobal.ningxia.downdata.imp.RemoteImpl;
import com.sinoglobal.ningxia.frame.FlyApplication;
import com.sinoglobal.ningxia.http.Code;
import com.sinoglobal.ningxia.thread.ItktAsyncTaskWithDialog;
import com.sinoglobal.ningxia.utils.FlyUtil;
import com.sinoglobal.ningxia.utils.ItktLog;
import com.sinoglobal.ningxia.utils.ToastBuilder;

import java.util.ArrayList;

/**
 * @author qiwx
 * @createdate 2014-6-26 下午1:55:23
 * @Description: 节目剧集
 */
public class ProgramHistoryFragment extends Fragment implements
        AbOnListViewListener {
    private View view;// 剧集view
    private ArrayList<programInfoVo> listData;
    private AbPullListView historyList;
    ProgramHistoryAdapter adapter;
    int listViewHeight = 10;// listview divider线高度
    LinearLayout comment;// 评论View
    ItktAsyncTaskWithDialog<String, Void, ProgramListVo> mTask;
    private String programId; // 节目id
    private byte limt1 = 0;// 请求页数
    private byte limt2;// 请求条数，此值默认给个0
    private TextView noData;// 没有数据时调用
    private static Context mContext;
    private ProgressBar pb;
    private Context context;

    public static ProgramHistoryFragment getIntance(Context context, String id) {
        ProgramHistoryFragment fragment = new ProgramHistoryFragment();
        Bundle bundle = new Bundle();
        bundle.putString("id", id);
        fragment.setArguments(bundle);
        mContext = context;
        return fragment;

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity();
        Bundle args = getArguments();
        if (args != null)
            programId = args.getString("id");
        listData = new ArrayList<programInfoVo>();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        view = LayoutInflater.from(mContext).inflate(R.layout.refresh_list,
                null);
        init();
        loadData();
        return view;
    }

    private void init() {
        pb = (ProgressBar) view.findViewById(R.id.programHistory);
        listData = new ArrayList<programInfoVo>();
        noData = (TextView) view.findViewById(R.id.noData);
        comment = (LinearLayout) view.findViewById(R.id.comment);
        comment.setVisibility(View.GONE);
        historyList = (AbPullListView) view.findViewById(R.id.refreshList);
        historyList.getHeaderView().setHeaderProgressBarDrawable(
                this.getResources().getDrawable(R.drawable.progress_circular));
        historyList.getFooterView().setFooterProgressBarDrawable(
                this.getResources().getDrawable(R.drawable.progress_circular));
        historyList.setAbOnListViewListener(this);
        historyList.setDivider(null);
        historyList.setDividerHeight(listViewHeight);
        historyList.setPullRefreshEnable(false);
        historyList.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Intent go = new Intent();
                go.putExtra(programInfoVo.class.getName(),
                        listData.get(position - 1));
                //使用老版播放器
//				FlyUtil.intentForwardNetWork(mContext, VideoHActivity.class, go);
                //使用新版播放器3.0.7
//                FlyUtil.intentForwardNetWork(mContext, JCVDHActivity.class, go);
                FlyUtil.intentForwardNetWork(mContext, GSYVDHActivity.class, go);
            }
        });

    }

    private void loadData() {
        pb.setVisibility(View.VISIBLE);
        mTask = new ItktAsyncTaskWithDialog<String, Void, ProgramListVo>(
                getString(R.string.data_loading), true, false) {
            @Override
            public ProgramListVo before(String... params) throws Exception {
                // historyList.setPullRefreshEnable(false);
                // historyList.setPullLoadEnable(false);
                ProgramListVo vo = null;

                try {
                    vo = RemoteImpl.getInstance().getMicProgramList(params[0],
                            Byte.parseByte(params[1]),
                            Byte.parseByte(params[2]));
                } catch (Exception e) {
                    e.printStackTrace();
                    vo = null;
                }
                return vo;
            }

            @Override
            public void after(ProgramListVo result) {
                pb.setVisibility(View.GONE);
                if (null == result) {
                    return;
                }
                if (null != result) {
                    int code = result.getCode();
                    switch (code) {
                        case Code.CODE_SUCCESS:
                            if (result.getList().size() <= 0
                                    || null == result.getList()) {
                                return;
                            }
                            listData.addAll(result.getList());
                            ItktLog.i("=====listData.size()====" + listData.size());
                            if (adapter == null) {
                                adapter = new ProgramHistoryAdapter(context,
                                        listData);
                                historyList.setAdapter(adapter);
                            } else {
                                adapter.notifyDataSetChanged();
                            }
                            noData.setVisibility(View.GONE);
                            break;
                        case Code.CODE_NODATA:
                            if (listData.size() == 0) {
                                noData.setVisibility(View.VISIBLE);
                                noData.setText(getString(R.string.no_content));
                                noData.setOnClickListener(new View.OnClickListener() {

                                    @Override
                                    public void onClick(View v) {
                                        // loadData();
                                    }
                                });
                            } else {
                                historyList.setPullLoadEnable(false);
                                ToastBuilder.getIntance(FlyApplication.context).showToast(
                                        getString(R.string.noMoreMessage));
                                historyList.stopLoadMore();
                            }

                            break;
                    }
                }
                cancel(true);
            }

            @Override
            public void exception() {
                historyList.setPullRefreshEnable(true);
                historyList.setPullLoadEnable(true);
                pb.setVisibility(View.GONE);
            }
        };
        mTask.execute(programId, String.valueOf(limt1), String.valueOf(limt2));
    }

    // 剧集没有刷新功能
    @Override
    public void onRefresh() {
    }

    @Override
    public void onLoadMore() {
        ++limt1;
        loadData();
    }

}
